package main

import (
	"context"
	"fmt"
	"log"
	"net"

	"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
	"google.golang.org/grpc"

	clusterservice "github.com/envoyproxy/go-control-plane/envoy/service/cluster/v3"
	discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
	endpointservice "github.com/envoyproxy/go-control-plane/envoy/service/endpoint/v3"
	listenerservice "github.com/envoyproxy/go-control-plane/envoy/service/listener/v3"
	routeservice "github.com/envoyproxy/go-control-plane/envoy/service/route/v3"
	xds "github.com/envoyproxy/go-control-plane/pkg/server/v3"
)

func main() {
	snapshotCache := cache.NewSnapshotCache(false, cache.IDHash{}, nil)
	server := xds.NewServer(context.Background(), snapshotCache, nil)
	grpcserver := grpc.NewServer()
	l, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatalln(err.Error())
	}
	discovery.RegisterAggregatedDiscoveryServiceServer(grpcserver, server)
	clusterservice.RegisterClusterDiscoveryServiceServer(grpcserver, server)
	endpointservice.RegisterEndpointDiscoveryServiceServer(grpcserver, server)
	routeservice.RegisterRouteDiscoveryServiceServer(grpcserver, server)
	listenerservice.RegisterListenerDiscoveryServiceServer(grpcserver, server)
	go func() {
		if err := grpcserver.Serve(l); err != nil {
			// error handle
			fmt.Println(err.Error())
		}
	}()
}
